from django.db import models
import datetime
from sync.models.sf_object import SFObject

class Picklist(models.Model):
    sf_object = models.CharField(max_length=100)
    sf_field = models.CharField(max_length=100)
    active = models.BooleanField()
    defaultValue  = models.BooleanField()
    label  = models.CharField(max_length=255)
    value = models.CharField(max_length=255)
    validFor = models.CharField(max_length=255,null=True)
    position=models.IntegerField(default=0)
    def __unicode__(self):
        return self.label
    @staticmethod
    def get_picklist(sf_object,field):
        p_list=Picklist.objects.filter(sf_object=sf_object,
                                sf_field=field,active=True)
        
        dflt=False
        l=[]
        for p in p_list:
            if p.defaultValue:
                dflt=p.value 
            l.append((p.value,p.label))
        if not dflt:
            l.insert(0,('',''))
        return l 
    @staticmethod
    def get_picklist_dflt(sf_object,field,blank_choice):
        p_list=Picklist.objects.filter(sf_object=sf_object,
                                sf_field=field,active=True)
        
        dflt=False
        l=[]
        for p in p_list:
            if p.defaultValue:
                dflt=p.value 
            l.append((p.value,p.label))
        
        if not dflt:
            if blank_choice:
                l.insert(0,('',''))
            return l,None
        else:
            return l,dflt
    @staticmethod         
    def is_bit_on(base64_str, bit_pos):
        if not base64_str:
            return False
        import base64
        """Test if bit at position bit_pos in on.
    
        Assumptions: bit_pos is zero-based, the order of the bits
        is "left-to-right" - bit 0 is the most signifcant bit in
        base64_str.
        """
        # convert from base64 to a byte array
        byte_str = base64.b64decode(base64_str)
        num_bytes = len(byte_str)
    
        if 0 >bit_pos:  # sanity check
            raise IndexError('bit_pos in out of range.')
        if  bit_pos >= num_bytes * 8:   
            return False
        # calculate the position of the byte that contains the tested bit,
        # assuming the left-most byte is the first.
        byte_pos = bit_pos / 8
    
        # the index of the tested bit in the byte containing it
        bit_in_byte_pos = bit_pos % 8
    
        # calculate the integer value of the byte
        byte = ord(byte_str[byte_pos])
    
        # compare the byte to a bit mask
        return byte & (0x80 >> bit_in_byte_pos) != 0   

    @staticmethod         
    def get_valid_for(sf_object,sf_field,val,
                      d_sf_object,d_sf_field):
        #print val
        pl=Picklist.objects.get(sf_object=sf_object,
                                   sf_field=sf_field,
                                   value=val)
        
        plist=Picklist.objects.filter(sf_object=d_sf_object,
                                   sf_field=d_sf_field)
        
        dlist=[]
        for p in plist:
            if Picklist.is_bit_on(p.validFor,pl.position):
                dlist.append(p) 
        return dlist
        
    class Meta:
        db_table = 'sync_picklist'
        app_label = 'sync'
        ordering = ["position"]
